#include "AppProperties.h"
#include "FlowController.h"
#include "View\Splash.h"
#include "View\PopupManager.h"
#include "View\StatisticWindow.h"
#include <gtk.h>
//#include <libintl.h>

//int main(int argc, char* argv[])
//{
//	//bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
//	//bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
//	//textdomain (GETTEXT_PACKAGE);
//
//	gtk_init(&argc, &argv);
//
//	//GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
//
//	//gtk_window_set_title(GTK_WINDOW(window), "Straitex");
//
//	//gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
//	//
//	////gtk_widget_show(window);
//	//
//	//FLOW_CONTROLLER->SetCurrentWindow(window);
//	////FlowController::GetInstance()->CloseCurrentWindow();
//
//	//GdkScreen* screen = NULL;
//	//screen = gtk_window_get_screen(GTK_WINDOW(window));
//	//gdk_screen_get_height(screen);
//////////////////////////////////////////////////////////////////	
//	gtk_set_locale();
//
//	Popup* alertPopup = new Popup("alert_popup_icon.png", "Attention!!!");
//
//	SPLASH->show();
//	POPUP_MANAGER->showPopup(alertPopup);
//
//
//	gtk_main();
//	
//	return 0;
//}
static void trayStatistic(GtkMenuItem *item, gpointer user_data);
static void traySplash(GtkMenuItem *item, gpointer user_data);
static void trayPopup(GtkMenuItem *item, gpointer user_data);
static void trayExit(GtkMenuItem *item, gpointer user_data);
static void trayIconActivated(GObject *trayIcon, gpointer data);
static void trayIconPopup(GtkStatusIcon *status_icon, guint button, guint32 activate_time, gpointer popUpMenu);
static void destroy (GtkWidget*, gpointer);
static gboolean delete_event (GtkWidget*, GdkEvent*, gpointer);
static gboolean window_state_event (GtkWidget *widget, GdkEventWindowState *event, gpointer user_data);

GtkStatusIcon *trayIcon;


int main(int argc, char *argv[])
{
    gtk_init (&argc, &argv);
    
    GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "GtkStatusIcon Example");
    gtk_widget_set_size_request (window, 200, -1);
    
    //set try icon file
    trayIcon  = gtk_status_icon_new_from_file ("icon.png");
    //set popup menu for tray icon
    GtkWidget *menu, *menuItemSplash, *menuItemExit, *menuItemPopup, *menuItemStatistic;
    menu = gtk_menu_new();
	menuItemStatistic	= gtk_menu_item_new_with_label ("Show Statistic");
    menuItemSplash		= gtk_menu_item_new_with_label ("Show/Hide Splash");
	menuItemPopup		= gtk_menu_item_new_with_label ("Show/Hide Popup");
    menuItemExit = gtk_menu_item_new_with_label ("Exit");
	g_signal_connect (G_OBJECT (menuItemStatistic), "activate", G_CALLBACK (trayStatistic), window);
    g_signal_connect (G_OBJECT (menuItemSplash), "activate", G_CALLBACK (traySplash), window);
	g_signal_connect (G_OBJECT (menuItemPopup), "activate", G_CALLBACK (trayPopup), window);
    g_signal_connect (G_OBJECT (menuItemExit), "activate", G_CALLBACK (trayExit), NULL);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuItemStatistic);
    gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuItemSplash);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuItemPopup);
    gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuItemExit);
    gtk_widget_show_all (menu);
    //set tooltip
    gtk_status_icon_set_tooltip (trayIcon, "Straitex");
    //connect handlers for mouse events
    g_signal_connect(GTK_STATUS_ICON (trayIcon), "activate", GTK_SIGNAL_FUNC (trayIconActivated), window);
    g_signal_connect(GTK_STATUS_ICON (trayIcon), "popup-menu", GTK_SIGNAL_FUNC (trayIconPopup), menu);
    gtk_status_icon_set_visible(trayIcon, FALSE); //set icon initially invisible
    
    
    GtkWidget *menuBar, *menuItemTopLvl, *mainMenu, *mainMenuItemExit;
    menuBar = gtk_menu_bar_new ();
    menuItemTopLvl = gtk_menu_item_new_with_label ("Menu");
    gtk_menu_shell_append (GTK_MENU_SHELL (menuBar), menuItemTopLvl);
    
    mainMenu = gtk_menu_new ();
    gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuItemTopLvl), mainMenu);
    mainMenuItemExit = gtk_menu_item_new_with_label ("Quit");
    g_signal_connect (G_OBJECT (mainMenuItemExit), "activate", G_CALLBACK (trayExit), NULL);
    gtk_menu_shell_append (GTK_MENU_SHELL (mainMenu), mainMenuItemExit);
    
    g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy), NULL);
    g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (delete_event), trayIcon);
    g_signal_connect (G_OBJECT (window), "window-state-event", G_CALLBACK (window_state_event), trayIcon);
    gtk_container_add (GTK_CONTAINER (window), menuBar);
    gtk_widget_show_all (window);

	gtk_widget_hide (GTK_WIDGET(window));
    gtk_status_icon_set_visible(GTK_STATUS_ICON(trayIcon), TRUE);

	//trayIconActivated(G_OBJECT(trayIcon), window);
    gtk_main ();
    return 0;
}

static void traySplash(GtkMenuItem *item, gpointer window) 
{
	if(!SPLASH->isVisible())
		SPLASH->show();
	else
		SPLASH->hide();
}

static void trayPopup(GtkMenuItem *item, gpointer window) 
{
	POPUP_MANAGER->showPopup(new Popup("alert_popup_icon.png", "Attention!!!"));
}

static void trayStatistic(GtkMenuItem *item, gpointer user_data)
{
	if(!STATISTIC_WINDOW->isVisible())
		STATISTIC_WINDOW->show();
	else
		STATISTIC_WINDOW->hide();
}

static void trayExit(GtkMenuItem *item, gpointer user_data) 
{
	gtk_status_icon_set_visible(GTK_STATUS_ICON(trayIcon), FALSE);
    printf("exit");
    gtk_main_quit();
}

static void trayIconActivated(GObject *trayIcon, gpointer window)
{
    gtk_widget_show(GTK_WIDGET(window));
    gtk_window_deiconify(GTK_WINDOW(window));
}

static void trayIconPopup(GtkStatusIcon *status_icon, guint button, guint32 activate_time, gpointer popUpMenu)
{
    gtk_menu_popup(GTK_MENU(popUpMenu), NULL, NULL, gtk_status_icon_position_menu, status_icon, button, activate_time);
}

static void destroy (GtkWidget *window, gpointer data)
{
  gtk_main_quit ();
}

static gboolean delete_event (GtkWidget *window, GdkEvent *event, gpointer data)
{
    return FALSE;
}

static gboolean window_state_event (GtkWidget *widget, GdkEventWindowState *event, gpointer trayIcon)
{
    if(event->changed_mask == GDK_WINDOW_STATE_ICONIFIED && (event->new_window_state == GDK_WINDOW_STATE_ICONIFIED || event->new_window_state == (GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_MAXIMIZED)))
    {
        gtk_widget_hide (GTK_WIDGET(widget));
        gtk_status_icon_set_visible(GTK_STATUS_ICON(trayIcon), TRUE);
    }
    else if(event->changed_mask == GDK_WINDOW_STATE_WITHDRAWN && (event->new_window_state == GDK_WINDOW_STATE_ICONIFIED || event->new_window_state == (GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_MAXIMIZED)))
    {
        gtk_status_icon_set_visible(GTK_STATUS_ICON(trayIcon), FALSE);
    }
    return TRUE;
}
